/**   
* TODO 
* @author zhanghui   
* @date 2016年4月28日 下午5:14:02  
*/
package com.nebula.upms.service.biz.impl;

import java.util.Date;
import java.util.Map;

import com.nebula.common.datasource.mybatis.pager.DataPager;
import com.nebula.common.datasource.mybatis.pager.Pager;
import com.nebula.common.util.UUIDGenerator;
import com.nebula.upms.service.biz.MessageService;
import com.nebula.upms.service.model.po.UpmsMessagePO;
import com.nebula.upms.service.model.po.UpmsMessagePOExample;
import com.nebula.upms.service.model.po.UpmsStaffReadMessagePO;
import com.nebula.upms.service.model.po.UpmsStaffReadMessagePOExample;
import com.nebula.upms.service.support.datasource.UpmsDs;
import org.springframework.stereotype.Service;
import com.nebula.common.framework.BaseService;


/** 
* TODO
* @author zhanghui
* @date 2016年4月28日 下午5:14:02  
*/
@Service("messageService")
public class MessageServiceImpl extends BaseService implements MessageService {

	/**
	* Description:
	* @param params
	* @param pager
	* @return
	**/
	@Override
	public DataPager<UpmsMessagePO> getUpmsMessagePOListForPager(Map<String, Object> params, Pager pager) {
		UpmsMessagePOExample example=new UpmsMessagePOExample();
		UpmsMessagePOExample.Criteria c=example.createCriteria();
		if(null!=params.get("title")){
			c.andTitleLike(params.get("title")+"%");
		}
		if(null!=params.get("status")){
			c.andStatusEqualTo(params.get("status").toString());
		}
		example.setOrderByClause("create_time desc");
		DataPager<UpmsMessagePO> dp= UpmsDs.instance().mybatis().ds().mapper().selectDataPager(example,pager);
		return dp;
	}

	/**
	* Description:
	* @param upmsMessagePO
	**/
	@Override
	public void saveUpmsMessagePO(UpmsMessagePO upmsMessagePO) {
		upmsMessagePO.setMessageId(UUIDGenerator.random32UUID());
		upmsMessagePO.setCreateTime(new Date().getTime());
		UpmsDs.instance().mybatis().ds().mapper().insert(upmsMessagePO);
	}

	/**
	* Description:
	* @param upmsMessagePO
	**/
	@Override
	public void updateUpmsMessagePO(UpmsMessagePO upmsMessagePO) {
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(upmsMessagePO);
	}

	/**
	* Description:
	* @param messageId
	* @return
	**/
	@Override
	public UpmsMessagePO getUpmsMessagePO(String messageId) {
		UpmsMessagePO po=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsMessagePO.class, messageId);
		return po;
	}

	/**
	* Description:
	* @param messageId
	* @param status
	**/
	@Override
	public void messageChange(String messageId, String status) {
		UpmsMessagePO upmsMessagePO=UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsMessagePO.class, messageId);
		upmsMessagePO.setStatus(status);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(upmsMessagePO);
		
	}

	/**
	* Description:
	* @param messageId
	**/
	@Override
	public void removeUpmsMessagePO(String messageId) {
		UpmsStaffReadMessagePOExample example=new UpmsStaffReadMessagePOExample();
		example.createCriteria().andMessageIdEqualTo(messageId);
		UpmsDs.instance().mybatis().ds().mapper().deleteByExample(example);
		UpmsDs.instance().mybatis().ds().mapper().deleteByPrimaryKey(UpmsMessagePO.class, messageId);
		
	}

	/**
	* Description:
	* @param params
	* @param pager
	* @return
	**/
	@Override
	public DataPager<UpmsMessagePO> selectStaffUnRedMessageList(Map<String, Object> params, Pager pager) {
		DataPager<UpmsMessagePO>  dp=UpmsDs.instance().mybatis().ds().mapper("PrivilegeMapper").selectDataPager("selectStaffUnRedMessageList", params, pager);
		return dp;
	}

	/**
	* Description:
	* @param staffId
	* @param messageId
	**/
	@Override
	public void saveStaffReadMessage(String staffId, String messageId) {
		UpmsStaffReadMessagePOExample example=new UpmsStaffReadMessagePOExample();
		example.createCriteria().andMessageIdEqualTo(messageId)
		.andStaffIdEqualTo(staffId);
		int count=(int)UpmsDs.instance().mybatis().ds().mapper().countByExample(example);
		if(count==0){
			UpmsStaffReadMessagePO po=new UpmsStaffReadMessagePO();
			po.setCreateTime(new Date().getTime());
			po.setMessageId(messageId);
			po.setStaffId(staffId);
			UpmsDs.instance().mybatis().ds().mapper().insertSelective(po);
		}
	}
}
